﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

using System.Data.Linq;
using System.Collections.Generic;

namespace Hisun.View.ReportQuery
{
    public partial class Report_CBTR_SJJG : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                ChildCenterList myChildCenterid = new ChildCenterList();

                int myid = (int)CurrentUserCenterId.责任部门ID;

                int[] MyCenterList = myChildCenterid.ChildCenterListItems(myid);
                
           
                var MyCenterTree = (from a in db.TABC_JS_CostCenter 
                                where MyCenterList.Contains(a.作业中心ID)
                                || a.作业中心ID == myid
                                select a).ToArray();
                
                this.TreeView_CenterTree.Nodes.Clear();
           
                MakeMenu(MyCenterTree);

                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist_Year(DropDownList_Year,Application["accountPeriod"].ToString());


            }
        }


        #region 生成作业中心目录数




 


        private void MakeMenu(TABC_JS_CostCenter[] menuSource)
        {
            foreach (var node in menuSource)
            {
                // 创建父节点
                if (node.上级作业中心ID == 0)
                {
                    TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                    // newNode.NavigateUrl = node.页面文件;
                    this.TreeView_CenterTree.Nodes.Add(newNode);
                    GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
                }
            }
        }


        /// <summary>
        /// 产生子节点
        /// </summary>
        /// <param name="childNodes"></param>
        /// <param name="fatherNode"></param>
        /// <param name="menuSource"></param>
        private void GenerateSubMenu(TABC_JS_CostCenter[] childNodes, TreeNode fatherNode, TABC_JS_CostCenter[] menuSource)
        {
            foreach (TABC_JS_CostCenter node in childNodes)
            {
                TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                // newNode.NavigateUrl = node.页面文件;

                fatherNode.ChildNodes.Add(newNode);

                GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
            }
        }
        /// <summary>
        /// 产生子节点的数据源
        /// </summary>
        /// <param name="father"></param>
        /// <param name="menuSource"></param>
        /// <returns></returns>
        private TABC_JS_CostCenter[] GetSubNodeSource(TABC_JS_CostCenter father, TABC_JS_CostCenter[] menuSource)
        {
            List<TABC_JS_CostCenter> subNodesSource = new List<TABC_JS_CostCenter>();
            foreach (TABC_JS_CostCenter node in menuSource)
            {
                //if (father.是否末级 == 'T')
                //{
                //    break;
                //}
                //else
                //{
                if (node.上级作业中心ID == father.作业中心ID)
                {
                    subNodesSource.Add(node);
                }
                //}
            }
            return subNodesSource.ToArray();
        }
        #endregion

        protected void TreeView_CenterTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            int MyCenterId = Convert.ToInt32(TreeView_CenterTree.SelectedNode.Value);
            string MyCenterName = TreeView_CenterTree.SelectedNode.Text;
            string MyYear = DropDownList_Year.SelectedValue.ToString();

            var isValid = from a in db.TABC_JS_AccountPeriods
                          where a.会计期间ID.Substring(0, 4) == MyYear
                          select a.会计期间ID;
            if (isValid.Count() == 0)
            {
                Response.Write("<script>alert('当前录入的会计年度值不存在');</script>");
                return;
            
            }
            CostReport MyReport = new CostReport();


            string UserName = Session[ServerVars.sysSessionUserName].ToString();
            string myHtmlReport = "";
            myHtmlReport = MyReport.WriteExcel_Report_CBTR_SJJG(MyCenterId, MyCenterName, MyYear, UserName);
            Response.Redirect("/Report/UserReport/" + myHtmlReport + ".html");



        }


    }
}
